package com.course.server.config;

import com.course.server.utils.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice  //aop
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult<Object> error(Exception e) {
        System.out.println("全局异常处理");
        e.printStackTrace();
        return ResponseResult.fail(500,"服务端异常");
    }

    //特定异常处理ArithmeticException
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResponseResult<Object> error(ArithmeticException e) {
        System.out.println("特定.....");
        e.printStackTrace();
        return ResponseResult.fail(500,"服务端异常");
    }

    //自定义异常处理Exception
    @ExceptionHandler(CourseException.class)
    @ResponseBody
    public ResponseResult<Object> error(CourseException e) {
        e.printStackTrace();
        return ResponseResult.fail(e.getCode(),e.getMsg());
    }

}
